﻿
Public Class frmMain
    'Dim allFonts As New Drawing.Text.InstalledFontCollection
    Dim random As New Random
    Dim oCanvas As New PictureBox
    Dim oBMP As New Bitmap(50, 50)
    Dim oCRange As New Collection
    Dim iCColor As Integer = 0
    'Dim Color1 As Color = Color.LightCyan
    'Dim Color2 As Color = Color.LightBlue
    Dim bInFormLoad As Boolean = True
    Dim CC As New ColorConverter

    Sub SetColorRange()
        'cmbColors.Items.Add("Red")
        'cmbColors.Items.Add("Blue")
        'cmbColors.Items.Add("Yellow")
        'cmbColors.Items.Add("Green")
        'cmbColors.Items.Add("Orange")
        'cmbColors.Items.Add("Purple")
        'cmbColors.Items.Add("Black & White")
        'cmbColors.Items.Add("Greyscale")
        'cmbColors.Items.Add("American")
        'cmbColors.Items.Add("British")
        'cmbColors.Items.Add("Jamaican")
        oCRange.Clear()


        Select Case cmbColors.Text.ToString.ToUpper.Trim
            Case "BLACK & WHITE"
                oCRange.Add("#ffffff")
                oCRange.Add("#000000")
                oCRange.Add("#ffffff")
                oCRange.Add("#000000")
                oCRange.Add("#ffffff")
                oCRange.Add("#000000")
            Case "ORANGE"
                oCRange.Add("#FFAA00")
                oCRange.Add("#BF8F30")
                oCRange.Add("#A66F00")
                oCRange.Add("#FFBF40")
                oCRange.Add("#FFD073")
                oCRange.Add("#FFAA00")
                oCRange.Add("#BF8F30")
                oCRange.Add("#A66F00")
                oCRange.Add("#FFBF40")
                oCRange.Add("#FFD073")
            Case "YELLOW"
                oCRange.Add("#FFFF00")
                oCRange.Add("#BFBF30")
                oCRange.Add("#A6A600")
                oCRange.Add("#FFFF40")
                oCRange.Add("#FFFF73")
                oCRange.Add("#FFFF00")
                oCRange.Add("#BFBF30")
                oCRange.Add("#A6A600")
                oCRange.Add("#FFFF40")
                oCRange.Add("#FFFF73")
            Case "RED"
                oCRange.Add("#FF0000")
                oCRange.Add("#BF3030")
                oCRange.Add("#A60000")
                oCRange.Add("#FF4040")
                oCRange.Add("#FF7373")
                oCRange.Add("#FF0000")
                oCRange.Add("#BF3030")
                oCRange.Add("#A60000")
                oCRange.Add("#FF4040")
                oCRange.Add("#FF7373")
            Case "BLUE"
                oCRange.Add("#1240AB")
                oCRange.Add("#2A4480")
                oCRange.Add("#06266F")
                oCRange.Add("#4671D5")
                oCRange.Add("#6C8CD5")
                oCRange.Add("#1240AB")
                oCRange.Add("#2A4480")
                oCRange.Add("#06266F")
                oCRange.Add("#4671D5")
                oCRange.Add("#6C8CD5")
            Case "PURPLE"
                oCRange.Add("#7109AA")
                oCRange.Add("#5F2580")
                oCRange.Add("#48036F")
                oCRange.Add("#9F3ED5")
                oCRange.Add("#AD66D5")
                oCRange.Add("#7109AA")
                oCRange.Add("#5F2580")
                oCRange.Add("#48036F")
                oCRange.Add("#9F3ED5")
                oCRange.Add("#AD66D5")
            Case "AQUA"
                oCRange.Add("#009999")
                oCRange.Add("#33CCCC")
                oCRange.Add("#5CCCCC")
                oCRange.Add("#006363")
                oCRange.Add("#1D7373")
                oCRange.Add("#009999")
                oCRange.Add("#33CCCC")
                oCRange.Add("#5CCCCC")
                oCRange.Add("#006363")
                oCRange.Add("#1D7373")
            Case "GREEN"
                oCRange.Add("#036303")
                oCRange.Add("#193619")
                oCRange.Add("#002400")
                oCRange.Add("#3DC03D")
                oCRange.Add("#6AC06A")
                oCRange.Add("#036303")
                oCRange.Add("#193619")
                oCRange.Add("#002400")
                oCRange.Add("#3DC03D")
                oCRange.Add("#6AC06A")
            Case "JAMAICAN"
                oCRange.Add("#009900")
                oCRange.Add("#FFCC00")
                oCRange.Add("#000000")
                oCRange.Add("#009900")
                oCRange.Add("#FFCC00")
                oCRange.Add("#000000")
                oCRange.Add("#009900")
                oCRange.Add("#FFCC00")
                oCRange.Add("#000000")
                oCRange.Add("#000000")
            Case "BRITISH"
                oCRange.Add("#cf142b")
                oCRange.Add("#00247d")
                oCRange.Add("#ffffff")
                oCRange.Add("#cf142b")
                oCRange.Add("#00247d")
                oCRange.Add("#ffffff")
                oCRange.Add("#cf142b")
                oCRange.Add("#00247d")
                oCRange.Add("#ffffff")
                oCRange.Add("#ffffff")
            Case "AMERICAN"
                '# american
                oCRange.Add("#ffffff")
                oCRange.Add("#002664")
                oCRange.Add("#bb133e")
                oCRange.Add("#ffffff")
                oCRange.Add("#002664")
                oCRange.Add("#bb133e")
                oCRange.Add("#ffffff")
                oCRange.Add("#002664")
                oCRange.Add("#bb133e")
            Case "PINK"
                oCRange.Add("#CD0074")
                oCRange.Add("#992667")
                oCRange.Add("#85004B")
                oCRange.Add("#E6399B")
                oCRange.Add("#E667AF")
                oCRange.Add("#CD0074")
                oCRange.Add("#992667")
                oCRange.Add("#85004B")
                oCRange.Add("#E6399B")
                oCRange.Add("#E667AF")
            Case "GREYSCALE"
                For i As Integer = 0 To 25
                    Dim igrey As Integer = RandomNumber(1, 254)
                    oCRange.Add(CC.ConvertToString(igrey & ", " & igrey & ", " & igrey))
                Next
            Case "RANDOM"
                For i As Integer = 0 To 4
                    oCRange.Add(CC.ConvertToString(GetRandomColor()))
                Next

        End Select

        

        


    End Sub
    Function GetRangeColor() As String
        'GetRandomColor = RandomNumber(0, 255) & "," & RandomNumber(0, 255) & "," & RandomNumber(0, 255)
        If oCRange.Count <= 0 Then
            Return "#ffffff"
            Exit Function
        End If
        GetRangeColor = oCRange.Item(RandomNumber(1, oCRange.Count))
        Return GetRangeColor
    End Function
    Function GetRandomColor() As String
        GetRandomColor = RandomNumber(0, 255) & "," & RandomNumber(0, 255) & "," & RandomNumber(0, 255)
        Return GetRandomColor
    End Function
    Sub SizeControls()
        If Me.Width < 200 Then Exit Sub
        If Me.Height < 200 Then Exit Sub
        If Me.WindowState <> FormWindowState.Minimized Then
            btnSave.Width = Me.Width - 20
            btnSave.Left = 0
            btnReDraw.Width = Me.Width - 20
            btnReDraw.Left = 0
            cmbColors.Width = Me.Width - 20
            cmbColors.Left = 0
            cmbResolutions.Width = Me.Width - 20
            cmbResolutions.Left = 0
        End If


    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        bInFormLoad = True
        '#randomise colors

        SizeControls()

        oCanvas.Anchor = (AnchorStyles.Bottom And AnchorStyles.Left And AnchorStyles.Right And AnchorStyles.Bottom)
        oCanvas.SizeMode = PictureBoxSizeMode.CenterImage

        oCanvas.Top = 0
        oCanvas.Left = 0
        oCanvas.Width = Me.Width - 20
        oCanvas.Height = Me.Height - 50
        oBMP = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
        oCanvas.Image = oBMP

        Me.Controls.Add(oCanvas)

        '# add colors to combo box
        cmbColors.Items.Add("Random")
        cmbColors.Items.Add("Aqua")
        cmbColors.Items.Add("Red")
        cmbColors.Items.Add("Blue")
        cmbColors.Items.Add("Yellow")
        cmbColors.Items.Add("Green")
        cmbColors.Items.Add("Orange")
        cmbColors.Items.Add("Purple")
        cmbColors.Items.Add("Pink")
        cmbColors.Items.Add("Black & White")
        cmbColors.Items.Add("Greyscale")
        cmbColors.Items.Add("American")
        cmbColors.Items.Add("British")
        cmbColors.Items.Add("Jamaican")

        cmbColors.SelectedItem = "Random"

        SetResolutions()
        '# set tool tips.
        tt.SetToolTip(btnReDraw, "Click here to redraw the image.")
        tt.SetToolTip(btnSave, "Click here to save the image.")
        tt.SetToolTip(cmbColors, "Click here to select a colour scheme for the image.")
        tt.SetToolTip(cmbResolutions, "Click here to select a resolution for the image.")
        tt.SetToolTip(tbMaxFontSize, "Use the track bar to set the font size.")

        bInFormLoad = False
        OpenList()

    End Sub

    Sub SetResolutions()
        cmbResolutions.Items.Add("XGA - 1024 x 768")
        cmbResolutions.Items.Add("XGA+ - 1152 x 864")
        cmbResolutions.Items.Add("WXGA - 1280 x 768")
        cmbResolutions.Items.Add("WXGA - 1280 x 800")
        cmbResolutions.Items.Add("SXGA - 1280 x 960")
        cmbResolutions.Items.Add("SXGA - 1280 x 1024")
        cmbResolutions.Items.Add("WXGA - 1360 x 768")
        cmbResolutions.Items.Add("WXGA - 1366 x 768")
        cmbResolutions.Items.Add("WSXGA - 1440 x 900")
        cmbResolutions.Items.Add("HD+ - 1600 x 900")
        cmbResolutions.Items.Add("UXGA - 1600 x 1200")
        cmbResolutions.Items.Add("WSXGA+ - 1680 x 1050")
        cmbResolutions.Items.Add("FullHDTV - 1920 x 1080")
        cmbResolutions.Items.Add("WUXGA - 1920 x 1200")
        cmbResolutions.Items.Add("DEFAULT - 640 x 480")
        cmbResolutions.Items.Add("DEFAULT - 800 x 600")
        cmbResolutions.Items.Add("DEFAULT - 1024 x 600")
        cmbResolutions.Items.Add("DEFAULT - 1024 x 768")
        cmbResolutions.Items.Add("DEFAULT - 1152 x 864")
        cmbResolutions.Items.Add("DEFAULT - 1280 x 720")
        cmbResolutions.Items.Add("DEFAULT - 1280 x 768")
        cmbResolutions.Items.Add("DEFAULT - 1280 x 800")
        cmbResolutions.Items.Add("DEFAULT - 1280 x 960")
        cmbResolutions.Items.Add("DEFAULT - 1280 x 1024")
        cmbResolutions.Items.Add("DEFAULT - 1360 x 768")
        cmbResolutions.Items.Add("DEFAULT - 1366 x 768")
        cmbResolutions.Items.Add("DEFAULT - 1440 x 900")
        cmbResolutions.Items.Add("DEFAULT - 1600 x 900")
        cmbResolutions.Items.Add("DEFAULT - 1600 x 1200")
        cmbResolutions.Items.Add("DEFAULT - 1680 x 1050")
        cmbResolutions.Items.Add("DEFAULT - 1920 x 1080")
        cmbResolutions.Items.Add("DEFAULT - 1920 x 1200")
        cmbResolutions.Items.Add("DEFAULT - 2560 x 1440")
        cmbResolutions.Items.Add("Android - 640x480")
        cmbResolutions.Items.Add("iPhone - 320 x 480")
        cmbResolutions.Items.Add("iPad - 1024 x 1024")
        cmbResolutions.Items.Add("Current screen resolution.")
        cmbResolutions.SelectedItem = "Current screen resolution."
    End Sub
    Function GetResolution() As Point
        Select Case cmbResolutions.SelectedItem
            Case "XGA - 1024 x 768"
                GetResolution.X = 1024
                GetResolution.Y = 768
            Case "XGA+ - 1152 x 864"
                GetResolution.X = 1152
                GetResolution.Y = 864
            Case "WXGA - 1280 x 768"
                GetResolution.X = 1280
                GetResolution.Y = 768
            Case "WXGA - 1280 x 800"
                GetResolution.X = 1280
                GetResolution.Y = 800
            Case "SXGA - 1280 x 960"
                GetResolution.X = 1280
                GetResolution.Y = 960
            Case "SXGA - 1280 x 1024"
                GetResolution.X = 1280
                GetResolution.Y = 1024
            Case "WXGA - 1360 x 768"
                GetResolution.X = 1360
                GetResolution.Y = 768
            Case "WXGA - 1366 x 768"
                GetResolution.X = 1366
                GetResolution.Y = 768
            Case "WSXGA - 1440 x 900"
                GetResolution.X = 1440
                GetResolution.Y = 900
            Case "HD+ - 1600 x 900"
                GetResolution.X = 1600
                GetResolution.Y = 900
            Case "UXGA - 1600 x 1200"
                GetResolution.X = 1600
                GetResolution.Y = 1200
            Case "WSXGA+ - 1680 x 1050"
                GetResolution.X = 1680
                GetResolution.Y = 1050
            Case "FullHDTV - 1920 x 1080"
                GetResolution.X = 1920
                GetResolution.Y = 1080
            Case "WUXGA - 1920 x 1200"
                GetResolution.X = 1920
                GetResolution.Y = 1200
            Case "DEFAULT - 640 x 480"
                GetResolution.X = 640
                GetResolution.Y = 480
            Case "DEFAULT - 800 x 600"
                GetResolution.X = 800
                GetResolution.Y = 600
            Case "DEFAULT - 1024 x 600"
                GetResolution.X = 1024
                GetResolution.Y = 600
            Case "DEFAULT - 1024 x 768"
                GetResolution.X = 1024
                GetResolution.Y = 768
            Case "DEFAULT - 1152 x 864"
                GetResolution.X = 1152
                GetResolution.Y = 864
            Case "DEFAULT - 1280 x 720"
                GetResolution.X = 1280
                GetResolution.Y = 720
            Case "DEFAULT - 1280 x 768"
                GetResolution.X = 1280
                GetResolution.Y = 768
            Case "DEFAULT - 1280 x 800"
                GetResolution.X = 1280
                GetResolution.Y = 800
            Case "DEFAULT - 1280 x 960"
                GetResolution.X = 1280
                GetResolution.Y = 800
            Case "DEFAULT - 1280 x 1024"
                GetResolution.X = 1280
                GetResolution.Y = 1024
            Case "DEFAULT - 1360 x 768"
                GetResolution.X = 1360
                GetResolution.Y = 768
            Case "DEFAULT - 1366 x 768"
                GetResolution.X = 1366
                GetResolution.Y = 768
            Case "DEFAULT - 1440 x 900"
                GetResolution.X = 1440
                GetResolution.Y = 900
            Case "DEFAULT - 1600 x 900"
                GetResolution.X = 1600
                GetResolution.Y = 900
            Case "DEFAULT - 1600 x 1200"
                GetResolution.X = 1600
                GetResolution.Y = 1200
            Case "DEFAULT - 1680 x 1050"
                GetResolution.X = 1680
                GetResolution.Y = 1050
            Case "DEFAULT - 1920 x 1080"
                GetResolution.X = 1920
                GetResolution.Y = 1080
            Case "DEFAULT - 1920 x 1200"
                GetResolution.X = 1920
                GetResolution.Y = 1200
            Case "DEFAULT - 2560 x 1440"
                GetResolution.X = 2560
                GetResolution.Y = 1440
            Case "Android - 640x480"
                GetResolution.X = 640
                GetResolution.Y = 480
            Case "iPhone - 320 x 480"
                GetResolution.X = 320
                GetResolution.Y = 480
            Case "iPad - 1024 x 1024"
                GetResolution.X = 1024
                GetResolution.Y = 1024
            Case "Current screen resolution."
                GetResolution.X = Screen.PrimaryScreen.Bounds.Width
                GetResolution.Y = Screen.PrimaryScreen.Bounds.Height
            Case Else
                GetResolution.X = Screen.PrimaryScreen.Bounds.Width
                GetResolution.Y = Screen.PrimaryScreen.Bounds.Height
        End Select

    End Function
    Sub OpenList()
        fdOpen.FileName = ""
        fdOpen.CheckFileExists = True
        fdOpen.CheckPathExists = True
        fdOpen.AddExtension = True
        fdOpen.Multiselect = False
        fdOpen.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
        If fdOpen.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub
        DrawImage()
    End Sub
    Sub DrawImage()
        SetColorRange()
        Dim pResolution As Point = GetResolution()
        oBMP = New Bitmap(pResolution.X, pResolution.Y)
        oCanvas.Image = oBMP
        FillBmp()
        If System.IO.File.Exists(fdOpen.FileName) = True Then
            Dim bPrintNegative As Boolean = False
            Me.Cursor = Cursors.WaitCursor

            For i As Integer = 0 To 500
                Dim objReader As New System.IO.StreamReader(fdOpen.FileName)
                Do While objReader.Peek() <> -1
                    DrawMyString(objReader.ReadLine(), bPrintNegative)
                    bPrintNegative = Not bPrintNegative
                Loop
                objReader = Nothing
            Next
            Me.Cursor = Cursors.Default
        Else
            MsgBox("File Does Not Exist")
            End
        End If
    End Sub
    Public Sub DrawMyString(ByVal sString As String, ByVal bPrintNegative As Boolean)
        ' Create string to draw.
        Dim oGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(oCanvas.Image)
        Dim drawString As [String] = sString.Trim
        Dim drawFont As New Font("Arial", RandomNumber(8, tbMaxFontSize.Value)) '# randomise font size

        'Dim drawBrush As New SolidBrush(IIf(bPrintNegative, Color1, Color2))
        Dim drawBrush As New SolidBrush(CC.ConvertFromString(GetRangeColor()))
        'Dim drawBrush As New SolidBrush(CC.ConvertFromString(GetRandomColor()))
        Dim drawPoint As New Point(RandomNumber(-50, oBMP.Width - 1), RandomNumber(-50, oBMP.Height - 1))
        oGraphics.DrawString(drawString, drawFont, drawBrush, drawPoint)
    End Sub
    Public Sub FillBmp()
        Dim oGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(oCanvas.Image)
        Dim width As Single = oBMP.Width
        Dim height As Single = oBMP.Height
        Dim drawBrush As New SolidBrush(CC.ConvertFromString(GetRangeColor()))
        'Dim cc As New ColorConverter
        oGraphics.FillRectangle(drawBrush, 0, 0, oCanvas.Width, oCanvas.Height)
    End Sub
    Private Function RandomNumber(ByVal min As Integer, ByVal max As Integer) As Integer
        Return random.Next(min, max)
    End Function

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If IsNothing(oCanvas.Image) Then
            Exit Sub
        End If
        fdSave.Filter = "JPG files (*.jpg)|*.jpg"
        If fdSave.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
            oCanvas.Image.Save(fdSave.FileName, Imaging.ImageFormat.Jpeg)
        End If
    End Sub
    Private Sub btnReDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReDraw.Click
        DrawImage()
    End Sub
    Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        SizeControls()
    End Sub
    Private Sub cmbColors_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbColors.SelectedIndexChanged
        If bInFormLoad Then Exit Sub
        If cmbColors.SelectedItem <> "" Then DrawImage()
    End Sub
    Private Sub cmbResolutions_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbResolutions.SelectedIndexChanged
        If bInFormLoad Then Exit Sub
        If cmbResolutions.SelectedItem <> "" Then DrawImage()
    End Sub
End Class